import { Flex } from "antd-mobile";
import React, { Component } from "react";
import { Modal } from "antd-mobile";
import { withRouter } from "react-router-dom";
import { isAuth } from "../../../../utils/cacheUtils";
import { get, post, _delete } from "../../../../utils/Axios";
import "./index.scss";
const alert = Modal.alert;
class PrivateFooter extends Component {
  state = {
    isFavorite: false,
  };

  // 取消收藏
  cancleFavorite = () => {
    const { houseHashCode } = this.props;
    if (houseHashCode) {
      const isLogin = isAuth();
      if (isLogin) {
        _delete(`/user/favorites/${houseHashCode}`);
      } else {
        this.props.history.push("/loginOrRegister");
      }
    }
  };

  // 加入收藏
  addFavorite = () => {
    const { houseHashCode } = this.props;
    if (houseHashCode) {
      const isLogin = isAuth();
      if (isLogin) {
        post(`/user/favorites/${houseHashCode}`);
      } else {
        alert(
          "提示",
          <div style={{ fontSize: "17px" }}>客观,登陆后才可以收藏呢~</div>,
          [
            { text: "取消", onPress: () => {} },
            {
              text: "去登陆",
              onPress: () => this.props.history.push("/loginOrRegister"),
            },
          ]
        );
      }
    }
  };

  componentDidMount() {
    const { houseHashCode } = this.props;
    if (houseHashCode) {
      const isLogin = isAuth();
      if (isLogin) {
        get(`/user/favorites/${houseHashCode}`).then((result) => {
          if (result.data.status === 200) {
            this.setState({
              isFavorite: result.data.body.isFavorite,
            });
          } else {
            console.log(result);
          }
        });
      }
    }
  }
  componentDidUpdate() {
    const { houseHashCode } = this.props;
    if (houseHashCode) {
      const isLogin = isAuth();
      if (isLogin) {
        get(`/user/favorites/${houseHashCode}`).then((result) => {
          if (result.data.status === 200) {
            this.setState({
              isFavorite: result.data.body.isFavorite,
            });
          } else {
            console.log(result);
          }
        });
      }
    }
  }

  render() {
    const { isFavorite } = this.state;
    return (
      <div className="PrivateFooterWrapper">
        <Flex>
          <Flex.Item>
            {isFavorite ? (
              <div
                className="collection"
                onClick={() => {
                  this.cancleFavorite();
                }}
              >
                <span className="iconfont icon-shoucang"></span> 已收藏
              </div>
            ) : (
              <div
                className="collection"
                onClick={() => {
                  this.addFavorite();
                }}
              >
                <span className="iconfont icon-shoucang1"></span> 收藏
              </div>
            )}
          </Flex.Item>
          <Flex.Item>
            <div className="onlineConsultation">在线咨询</div>
          </Flex.Item>
          <Flex.Item>
            <div className="reserveByPhone">电话预约</div>
          </Flex.Item>
        </Flex>
      </div>
    );
  }
}

export default withRouter(PrivateFooter);
